import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/* 生命绽放 */
export default class DLY_LifeBlooms extends AbilityBase {
    buffType: AppBuffType = {
        id: 'buff_DLY_LifeBlooms',
        art: this.art,
        name: this.abilityName,
        ubertip: '持续受到治疗',
        interval: 2,
        onInterval: this.buffInterval.bind(this),
        stackCountMax: -1,
        dur: 12,
        targetAttach: 'overhead',
        targetArt: 'Abilities\\Spells\\NightElf\\Tranquility\\TranquilityTarget.mdl',
    };
    static instance: DLY_LifeBlooms;
    constructor(id: string) {
        super(id);
        DLY_LifeBlooms.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.受到治疗(this.healEffect.bind(this));
    }
    buffInterval(buff: Buff) {
        let s = buff.getCaster();
        let t = buff.getParent();
        let lv = this.getAbilityLevel(s);
        let dmg = 18 * lv + UnitCommon.取法强(s) * (0.6 + 0.4 * lv);
        DamageSystemInstance.applyDotHeal(t, s, dmg, this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let lv = this.getAbilityLevel(source);
        BuffCommon.addBuff({
            target: source,
            caster: source,
            buffTypeId: this.buffType.id,
            ablityId: this.id,
            config: {
                stack: 3 + R2I(lv / 2),
            },
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    healEffect(d: DamageEventData) {
        if (!d.isDot) return;
        // 如果目标的生命小30%
        let per = GetUnitLifePercent(d.target);
        if (per > 30) return;
        let s = d.source;
        let buff = BuffUtil.getUnitBuff(s, this.buffType.id);
        if (buff == null) return;

        if (buff.stackCount == 0) {
            buff.destroy();
        } else {
            buff.stackCount -= 1;
        }
        let lv = this.getAbilityLevel(s);
        let dmg = ((18 * lv + UnitCommon.取法强(s) * (0.6 + 0.4 * lv)) * buff.getRemainingTime()) / 1000;
        DamageSystemInstance.applyHeal(d.target, s, dmg, this.id);
    }
}
